package com.aeasycredit.credit.tuef.api.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.aeasycredit.commons.lang.utils.DatesUtils;
import com.aeasycredit.credit.tuef.api.ExcelCrpApi;
import com.aeasycredit.credit.tuef.config.ExcelMockData;
import com.aeasycredit.credit.tuef.config.ExcelProperties;
import com.aeasycredit.credit.tuef.dto.CreditReportEnquiryBodyDto;
import com.aeasycredit.credit.tuef.dto.CreditReportEnquiryResultBodyDto;
import com.aeasycredit.credit.tuef.dto.CreditReportEnquiryResultDto;
import com.aeasycredit.credit.tuef.dto.ExcelCrpParamDto;
import com.aeasycredit.credit.tuef.manager.TuefFormatDataBuilder;
import com.aeasycredit.credit.tuef.model.TuReport;
import com.aeasycredit.credit.tuef.service.TuReportService;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

@RestController
public class ExcelCrpApiImpl implements ExcelCrpApi {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TuefFormatDataBuilder tuefFormatDataBuilder;
    
    @Autowired
    private TuReportService tuReportService;
    
    @Autowired
    private ExcelProperties excelProperties;
    
    @Override
    public CreditReportEnquiryResultDto enquiryCreditReport(ExcelCrpParamDto<CreditReportEnquiryBodyDto> requestParam) {
        logger.info("[征信報告查詢]requestParam:{}", JSONUtil.toJsonStr(requestParam));
        
        CreditReportEnquiryBodyDto creditReportEnquiryBody = requestParam.getBody();
        String idNo = creditReportEnquiryBody.getIdNo();
        ExcelMockData excelMockData = excelProperties.getMatchMockDataList().stream().filter(item -> idNo.matches(item.getIdCard())).findFirst().orElse(excelProperties.getDefaultMockData());
        logger.info("[征信報告查詢]excelMockData:{}", JSONUtil.toJsonStr(excelMockData));
        
        String tuefData = tuefFormatDataBuilder.build(excelMockData.getTuefBasicId());
        logger.info("[征信報告查詢]tuefData:{}", tuefData);
        
        TuReport tuReport = tuReportService.selectByPrimaryKey(excelMockData.getTuReportId());
        byte[] creditReportReadable = tuReport.getCreditReportReadable();
        creditReportReadable = replaceTuReportDate(creditReportReadable);
        
        String reqMsgId = StrUtil.format("MFF{}", RandomUtil.randomNumbers(26));
        CreditReportEnquiryResultBodyDto body = new CreditReportEnquiryResultBodyDto();
        body.setDataContent(tuefData);
        body.setFileName(StrUtil.format("{}.rtf", reqMsgId));
        body.setFileContent(creditReportReadable);
        body.setReturnCode("GWMSG00000");
        body.setReturnDesc("Success");
        body.setRspMsgId(null);
        body.setReqMsgId(reqMsgId);
        
        CreditReportEnquiryResultDto result = new CreditReportEnquiryResultDto();
        result.setBody(body);
        result.setHeader(requestParam.getHeader());
        
        logger.info("[征信報告查詢]response:{}", JSONUtil.toJsonStr(result));
        return result;
    }
    
    /**
     * 
     * 替換可讀報告字節數組中的TU報告日期為當前日期時間<br>
     *
     * @param creditReportBytes 可讀報告字節數組
     * @return byte[] 替換後的可讀報告字節數組
     */
    private byte[] replaceTuReportDate(byte[] creditReportReadableBytes) {
        
        String creditReportReadable = new String(creditReportReadableBytes);
        String currentDate = DatesUtils.DDMMYYHHMMSS.toString();
        creditReportReadable = creditReportReadable.replaceFirst("DATE: ([0-9]{2}-[0-9]{2}-[0-9]{4})", "DATE: " + currentDate.substring(0, 10));
        creditReportReadable = creditReportReadable.replaceFirst("TIME: ([0-9]{2}:[0-9]{2})", "TIME: " + currentDate.substring(11, 16));
        logger.info("[征信報告查詢]creditReportReadable:{}", creditReportReadable);
        return creditReportReadable.getBytes();
    }
}
